package com.bjtime.hprinter.lib;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.bjtime.hprinter.ui.DesignerActivity;
import com.puty.sdk.PrinterInstance;

import java.lang.reflect.Method;

/**
 * Created by songgoo on 2017/8/20.
 */

public class BluetoothConnectActivityReceiver extends BroadcastReceiver {
    String strPsw = "1234";

    @Override
    public void onReceive(Context context, Intent intent)
    {
        // TODO Auto-generated method stub
        if (intent.getAction().equals(
                "android.bluetooth.device.action.PAIRING_REQUEST"))
        {
            BluetoothDevice btDevice = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            // byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
            // device.setPin(pinBytes);
            Log.i("tag11111", "ddd");
            try
            {
                abortBroadcast();
                //boolean ret =ClsUtils.setPin(btDevice.getClass(), btDevice, strPsw); // 手机和蓝牙采集器配对
                Method removeBondMethod = btDevice.getClass().getDeclaredMethod("setPin", new Class[]{byte[].class});
                Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice, new Object[]{strPsw.getBytes()});
                //ClsUtils.createBond(btDevice.getClass(), btDevice);
                //ClsUtils.cancelPairingUserInput(btDevice.getClass(), btDevice);
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            switch (device.getBondState()) {
                case BluetoothDevice.BOND_BONDING://正在配对
                    Log.d("BlueToothTestActivity", "正在配对......");
                    Toast.makeText(context,"正在配对......", Toast.LENGTH_SHORT).show();
                    break;
                case BluetoothDevice.BOND_BONDED://配对结束
                    Log.d("BlueToothTestActivity", "完成配对");
                    //abortBroadcast();
                    Toast.makeText(context,"完成配对", Toast.LENGTH_SHORT).show();
                    break;
                case BluetoothDevice.BOND_NONE://取消配对/未配对
                    Log.d("BlueToothTestActivity", "取消配对");
                    Toast.makeText(context,"取消配对", Toast.LENGTH_SHORT).show();
                default:
                    break;
            }
        }else if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
            BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            if(btDevice.getAddress().equals(DesignerActivity.Companion.getDeviceaddress())){
                PrinterInstance.mPrinter = null;
                DesignerActivity.Companion.setDeviceaddress("");
                DesignerActivity.Companion.setConnect(true);
            }
            if (DesignerActivity.Companion.getBluetoothAdapter() != null) {
                DesignerActivity.Companion.getBluetoothAdapter().cancelDiscovery();
                DesignerActivity.Companion.getBluetoothAdapter().startDiscovery();
            }
            return;
        }
    }
}
